package jjn.round2;

/**
 * @author Jiang Jining
 * @since 2023-05-27 23:29
 */
public class LeetCode1093_SampleStats {
    public double[] sampleStats(int[] count) {
        int n = 0, min = -1, max = 0, mode = 0;
        long sum = 0;
        for (int i = 0; i < 256; ++i) {
            if (count[i] == 0) continue;
            n += count[i];
            sum += (long) i * count[i];
            if (min == -1) min = i;
            max = i;
            if (count[i] > count[mode]) mode = i;
        }
        double mean = (double) sum / n;
        int i = 0, j = 255;
        // 从左边删除最多 (n-1)/2 个数
        for (int t = n - 1 >> 1; t >= count[i]; t -= count[i++]) ;
        // 从右边删除最多 (n-1)/2 个数
        for (int t = n - 1 >> 1; t >= count[j]; t -= count[j--]) ;
        // 中间两位数(可能是同一个数)求平均
        double median = (i + j) / 2.0;
        return new double[]{min, max, mean, median, mode};
    }
}
